#==============================================================================
#  _   _                         ____                _             
# | | | | ___  _ __ ___   ___   / ___|___  _ __ ___ (_)_ __   __ _ 
# | |_| |/ _ \| '_ ` _ \ / _ \ | |   / _ \| '_ ` _ \| | '_ \ / _` |
# |  _  | (_) | | | | | |  __/ | |__| (_) | | | | | | | | | | (_| |
# |_| |_|\___/|_| |_| |_|\___|  \____\___/|_| |_| |_|_|_| |_|\__, |
#                                                            |___/ 
#------------------------------------------------------------------------------
# Project:	Home Coming / 2D platform
# Author:	zx.longgi
# Time:		08/10-2010 
# Usage:	basic file read and write functions.
# Description:	a basic file operating class
# ClassName:	zfile
#==============================================================================
import os.path


class zfile:
	"""basic file read and write functions"""
	def open_read_and_close(self, pathname):
		if(os.path.exists(pathname)):
			fp = open(pathname, 'r')
			textlines = []
			try:
				for line in fp:
					#print line
					textlines.append(line)
				return textlines
			finally:
				fp.close()
		else:
			return False
			
	def open_save_and_close(self, pathname, lines=[]):
		fp = open(pathname, 'w')
		try:
			for line in lines:
				fp.writeline(line)
		finally:
			fp.close()
			
	def check_line_is_comment_or_not(self, cline=""):
		"""check if the input line a comment"""
		#strip first, to avoid errors
		cline = cline.strip();
		
		#check start
		if(cline[0:2] == "//"):
			return True
		else:
			return False
		
	def check_line_is_section_name(self, cline=""):
		"""check if the input line a section name"""
		#strip first, to avoid errors
		cline = cline.strip();
		
		#check start
		if(cline[0]=='[' and cline[-1]==']'):
			return True
		else:
			return False
	